package dao.impl;

import dao.GenericDao;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import util.HibernateUtil;

public abstract class GenericDaoImpl<T> implements GenericDao<T> {

    public void save(Object entity) {
        Session hibernateSession = HibernateUtil.getSession();
        hibernateSession.saveOrUpdate(entity);
    }

    public void delete(Object entity) {
        Session hibernateSession = HibernateUtil.getSession();
        hibernateSession.delete(entity);
    }

    public List<T> getAll(Class clazz) {
        Session hibernateSession = HibernateUtil.getSession();
        List<T> lst = null;
        Query query = hibernateSession.createQuery("from " + clazz.getName());
        lst = query.list();
        return lst;
    }

    public T getById(Class clazz, int id) {
        Session hibernateSession = HibernateUtil.getSession();
        T t = (T) hibernateSession.get(clazz, id);
        return t;
    }
}
